<template>

  <div v-loading="loading" element-loading-text="文件上传中......">

    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="关键字">
              <el-input style="width: 100%" v-model="searchObj.message" placeholder="请输信息关键字"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <!--添加按钮-->
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
    </div>

    <!--添加、修改弹出框-->
    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :model="sysNotice" label-width="150px" size="small" style="padding-right: 40px;">


        <el-form-item label="会诊编号" prop="consultationId">
          <el-select v-model="sysNotice.consultationId" placeholder="请输入会诊编号">
            <el-option
              v-for="item in this.orderList"
              :key="item.id"
              :label="item.consultationId"
              :value="item.consultationId"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="数据类型">
          <el-select v-model="sysNotice.type" placeholder="选择发送数据类型">
            <el-option label="信息" value="text" selected></el-option>
            <el-option label="图片" value="image"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="信息" prop="message" v-if="sysNotice.type === 'text'">
          <el-input type="textarea" v-model="sysNotice.message"/>
        </el-form-item>

        <el-form-item label="图片" prop="message" v-if="sysNotice.type === 'image'">
          <el-upload
            class="upload-demo"
            name="uploadImage"
            action="http://localhost:8085/lovePet/system/upload/uploadImage"
            :limit="1"
            :on-success="handleImageSuccess"
            :before-upload="handleBeforeUpload"
            :show-file-list="false"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>


        <el-form-item label="发送人员">
          <el-select v-model="sysNotice.people">
            <el-option label="用户" value="1"></el-option>
            <el-option label="医生" value="2"></el-option>
          </el-select>
        </el-form-item>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
      @selection-change="handleSelectionChange">

      <!--&lt;!&ndash; 复选框 &ndash;&gt;-->
      <!--<el-table-column type="selection"/>-->

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>


      <el-table-column prop="consultationId" label="会诊编号"/>


      <el-table-column prop="message" label="信息">
        <template slot-scope="scope">

          <viewer v-if="scope.row.type === 'text'">
            {{ scope.row.message }}
          </viewer>

          <viewer v-v-if="scope.row.type === 'image'">
            <img :src="scope.row.message" alt="" style="height: 80px"/>
          </viewer>

        </template>
      </el-table-column>


      <el-table-column prop="licenseNumber" label="医生名称" :formatter="doctorFormatter"/>

      <el-table-column prop="userId" label="用户名称" :formatter="userFormatter"/>

      <el-table-column prop="type" label="数据类型"/>


      <!--<el-table-column prop="price" label="问诊价格"/>-->

      <!--<el-table-column prop="message" label="图片">-->
      <!--  <template slot-scope="scope">-->
      <!--    <viewer>-->
      <!--      <img :src="scope.row.message" alt="" style="height: 80px"/>-->
      <!--    </viewer>-->
      <!--  </template>-->
      <!--</el-table-column>-->

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"/>
        </template>
      </el-table-column>
    </el-table>


    <!--分页组件-->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />


  </div>

</template>

<script>

  import api from '@/api/sysNotice/sysNotice.js';

  import userapi from '@/api/user/user.js';

  import doctorapi from '@/api/sysDoctor/sysDoctor.js';

  import orderapi from '@/api/sysOrder/sysOrder.js';


  export default {
    data() {
      return {
        // listLoading:true, //  加载中的提示语.....
        list: [],
        total: 0,
        page: 1,
        limit: 4,
        searchObj: {
          name: ''
        },

        sysNotice: {},

        dialogVisible: false,
        // 选中的数据
        selectValueData: [],
        fileList: [],
        loading: false,

        dialogVisible2: false,
        expertiseItem: '',

        people: '',

        userList: [],
        doctorList: [],
        orderList: [],

      };
    },

    created() {
      this.fetchData();
      this.findAll();
    },

    methods: {

      //根据id获取用户
      userFormatter(row) {
        let id = row.userId;
        let name = '';
        for (let i = 0; i < this.userList.length; i++) {
          if (this.userList[i].id === id) {
            name = this.userList[i].name;
            break;
          }
        }
        return name;
      },

      doctorFormatter(row) {
        let licenseNumber = row.licenseNumber;
        let name = '';
        for (let i = 0; i < this.doctorList.length; i++) {
          if (this.doctorList[i].licenseNumber === licenseNumber) {
            name = this.doctorList[i].name;
            break;
          }
        }
        return name;
      },


      findAll() {
        userapi.findAll().then((response) => {
          this.userList = response.data;
        });
        doctorapi.findAll().then((response) => {
          this.doctorList = response.data;
        });
        orderapi.findAll().then((response) => {
          this.orderList = response.data;
        });
      },

      //分页查询
      fetchData(pageNum = 1) {
        this.page = pageNum;
        console.log(this.searchObj);
        api.getPageList(this.page, this.limit, this.searchObj).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;

        })
      },

      //触发重置
      resetData() {
        // 清空表单
        this.searchObj = {};
        // 再次刷新列表数据
        this.fetchData();
      },


      //删除
      removeDataById(id) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(response1 => {
          api.removeDataById(id).then(response2 => {

            this.$message.success("删除成功");
            // 刷新页面
            this.fetchData();

          });
        });
      },

      // 打开弹框
      add() {

        // 将 控制弹框的 属性设置为 true
        this.dialogVisible = true;
        // 清空表单
        this.sysNotice = {};

      },

      // 根据id去回显数据
      edit(id) {
        //2.赋值
        api.getSysDataById(id).then(response => {
          this.sysNotice = response.data;
        });
        //
        // if (this.sysNotice.licenseNumber === '' || this.sysNotice.licenseNumber === null){
        //   this.sysNotice.people = '1';
        // }else {
        //   this.sysNotice.people = '2';
        // }

        //1.弹框
        this.dialogVisible = true;

      },

      // 添加或者修改 具体功能
      saveOrUpdate() {
        const targetConsultationId = this.sysNotice.consultationId;
        for (let i = 0; i < this.orderList.length; i++) {
          if (this.orderList[i].consultationId === targetConsultationId ) {
            this.sysNotice.userId = '0';
            this.sysNotice.licenseNumber = '';
            if (this.sysNotice.people === '1') {
              // 执行选择为用户时的逻辑
              this.sysNotice.userId = this.orderList[i].userId;
            } else{
              // 执行选择为医生时的逻辑
              this.sysNotice.licenseNumber = this.orderList[i].licenseNumber;
            }

            break;

          }
        }


        if (this.sysNotice.id != null) {
          this.updateData();
        } else {
          this.addData();
        }
      },

      // 实现添加功能
      addData() {
        api.saveData(this.sysNotice)
          .then(response => {
            this.$message({
              type: "success",
              message: "添加成功!",
            });
            // 关闭弹框
            this.dialogVisible = false;
            // 刷新数据
            this.fetchData();

          })
      },

      updateData() {
        api.updateData(this.sysNotice)
          .then(response => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            // 关闭弹框
            this.dialogVisible = false;
            // 刷新数据
            this.fetchData();
          })
      },


      //图片上传成功的钩子函数
      handleImageSuccess(res, file) {

        console.log(res);  // 得到的是图片地址
        console.log(file);  // 得到是一个对象

        this.loading = false;
        if (file.response != "") {
          // this.sysMovie.image = file.response.data;
          this.sysNotice.message = file.response;
          this.fileList = [];
          console.log("=================" + this.sysNotice.message);
          this.$message({
            type: "info",
            message: "图片上传成功",
            duration: 6000,
          });

        } else {
          this.fileList = [];
          this.$message({
            type: "info",
            message: "图片上传失败",
            duration: 6000,
          });
        }
      },

      //文件开始上传,开始屏幕遮罩
      handleBeforeUpload() {
        this.loading = true;
      },



    }
  }
</script>

<style scoped>

</style>

